<?php

class App_Db_Script{
	
	public $errors = null;
	
	function App_Db_Script($module,$scriptname,$dbAdapter = null){
		$success = false;
		$this->errors = array();
		
		if(is_null($dbAdapter)){
			$db = Zend_Db_Table::getDefaultAdapter();
		} else {
			$db = $dbAdapter;
		}
		
		$obj_type = get_class($db);
		$db_type = strtoupper(substr($obj_type,strlen("Zend_Db_Adapter_")));
		
		$ddl_file = Zend_Registry::get("basepath")."/modules/".$module."/sql/".$db_type."/".$scriptname.".sql";
		
		
		if(file_exists($ddl_file)){
			$queries = explode(";",file_get_contents($ddl_file));
			
			try{
				foreach($queries as $query){
					if(trim($query) != ""){					
						$db->query($query);
					}
				}
				$success = true;						
			} catch (Exception $e){
				$this->errors[] = $e->getMessage();				
			}
			
		} else {
			$this->errors[] = "Script file is missing.";
		}		

		return $success;
	}
	
}